package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.gson.Gson;
import com.psa.tool.DBTool;

import java.util.ArrayList;
import java.util.List;

public class PDA_DefdocList implements ISqlObject, IJson {
    public String PK_ID = "";
    public String PK_DEFDOC = "";
    public String NAME = "";
    public String CONTENT = "";
    public int DATATYPE = 0;
    public int DR = 0;

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("pk_id", PK_ID.trim());
        values.put("pk_defdoc", PK_DEFDOC.trim());
        values.put("name", NAME.trim());
        values.put("content", CONTENT.trim());
        values.put("datatype", DATATYPE);
        values.put("dr", DR);
        return (int)dbTool.insert("PDA_DefdocList", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("pk_defdoc", PK_DEFDOC.trim());
        values.put("name", NAME.trim());
        values.put("content", CONTENT.trim());
        values.put("datatype", DATATYPE);
        values.put("dr", DR);
        String where = "pk_id = ?";
        String[] whereval = { PK_ID.trim() };
        return dbTool.update("PDA_DefdocList", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "pk_id = ?";
        String[] wherevalue = { PK_ID.trim() };
        return dbTool.delete("PDA_DefdocList", where, wherevalue);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            PDA_DefdocList pda_defdoc = new PDA_DefdocList();
            pda_defdoc.PK_ID = cursor.getString(0);
            pda_defdoc.PK_DEFDOC = cursor.getString(1);
            pda_defdoc.NAME = cursor.getString(2);
            pda_defdoc.CONTENT = cursor.getString(3);
            pda_defdoc.DATATYPE = cursor.getInt(4);
            pda_defdoc.DR = cursor.getInt(5);
            return pda_defdoc;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("PDA_DefdocList", "pk_id Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "PDA_DefdocList";
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }

    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public String toString() {
        return CONTENT;
    }

    public static List<ISqlObject> getDefdocListByType(String t) {
        PDA_DefdocList defdocList = new PDA_DefdocList();
        List<ISqlObject> defdocLists = new ArrayList<ISqlObject>();
        Cursor cursor = DBTool.getInstance().query("Select * From PDA_DefdocList Where pk_defdoc='"+ t + "'");
        while ((defdocList = (PDA_DefdocList)defdocList.full(cursor)) != null) {
            defdocLists.add(defdocList);
        }
        return  defdocLists;
    }
}
